package com.yang.ddd.domain.space.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.ddd.domain.space.entity.SpaceUser;
import com.yang.ddd.domain.space.repository.SpaceUserRepository;
import com.yang.ddd.domain.space.service.SpaceUserDomainService;
import com.yang.ddd.interfaces.dto.spaceuser.SpaceUserQueryRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Young
 * @description 针对表【space_user(空间用户关联)】的数据库操作Service实现
 * @createDate 2025-05-13 22:11:02
 */
@Service
public class SpaceUserDomainServiceImpl implements SpaceUserDomainService {

    @Resource
    private SpaceUserRepository spaceUserRepository;

    @Override
    public QueryWrapper<SpaceUser> getQueryWrapper(SpaceUserQueryRequest spaceUserQueryRequest) {
        QueryWrapper<SpaceUser> queryWrapper = new QueryWrapper<>();
        if (spaceUserQueryRequest == null) {
            return queryWrapper;
        }
        // 从对象中取值
        Long id = spaceUserQueryRequest.getId();
        Long spaceId = spaceUserQueryRequest.getSpaceId();
        Long userId = spaceUserQueryRequest.getUserId();
        String spaceRole = spaceUserQueryRequest.getSpaceRole();
        queryWrapper.eq(ObjUtil.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjUtil.isNotEmpty(spaceId), "spaceId", spaceId);
        queryWrapper.eq(ObjUtil.isNotEmpty(userId), "userId", userId);
        queryWrapper.eq(ObjUtil.isNotEmpty(spaceRole), "spaceRole", spaceRole);
        return queryWrapper;
    }

    @Override
    public boolean save(SpaceUser spaceUser) {
        return spaceUserRepository.save(spaceUser);
    }

    @Override
    public SpaceUser getById(long id) {
        return spaceUserRepository.getById(id);
    }

    @Override
    public boolean removeById(long id) {
        return spaceUserRepository.removeById(id);
    }

    @Override
    public SpaceUser getOne(SpaceUserQueryRequest spaceUserQueryRequest) {
        return spaceUserRepository.getOne(this.getQueryWrapper(spaceUserQueryRequest));
    }

    @Override
    public List<SpaceUser> list(SpaceUserQueryRequest spaceUserQueryRequest) {
        return spaceUserRepository.list(this.getQueryWrapper(spaceUserQueryRequest));
    }

    @Override
    public boolean updateById(SpaceUser spaceUser) {
        return spaceUserRepository.updateById(spaceUser);
    }
}




